package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class ImageEditor extends Shell {
	private Text text;
	private IconView canvas;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ImageEditor shell = new ImageEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public ImageEditor(Display display, String filePath, final OnImageSelectedListener listener) {
		this(display, listener);
		text.setText(filePath);
		canvas.setImage(filePath);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public ImageEditor(Display display, final OnImageSelectedListener listener) {
		super(display, SWT.DIALOG_TRIM);
		setLayout(new FormLayout());

		Label lblNewLabel = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.top = new FormAttachment(0, 13);
		fd_lblNewLabel.left = new FormAttachment(0, 10);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("\u56FE\u7247 : ");

		text = new Text(this, SWT.BORDER);
		text.setEditable(false);
		FormData fd_text = new FormData();
		fd_text.top = new FormAttachment(0, 10);
		fd_text.left = new FormAttachment(lblNewLabel, 6);
		text.setLayoutData(fd_text);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					text.setText(path);
				}
				canvas.setImage(path);
			}
		});
		fd_text.right = new FormAttachment(btnNewButton, -6);
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(lblNewLabel, -5, SWT.TOP);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null && text.getText().length() > 0) {
					listener.onImageSelected(text.getText());
				}
				dispose();
			}
		});
		FormData fd_btnNewButton_1 = new FormData();
		fd_btnNewButton_1.left = new FormAttachment(btnNewButton, 0, SWT.LEFT);
		fd_btnNewButton_1.bottom = new FormAttachment(100, -10);
		fd_btnNewButton_1.right = new FormAttachment(btnNewButton, 0, SWT.RIGHT);
		btnNewButton_1.setLayoutData(fd_btnNewButton_1);
		btnNewButton_1.setText("\u5B8C\u6210");

		canvas = new IconView(this);
		FormData fd_canvas = new FormData();
		fd_canvas.bottom = new FormAttachment(btnNewButton_1, -6);
		fd_canvas.top = new FormAttachment(btnNewButton, 4);
		fd_canvas.left = new FormAttachment(0, 10);
		fd_canvas.right = new FormAttachment(0, 434);
		canvas.setLayoutData(fd_canvas);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u9009\u62E9\u56FE\u7247");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public interface OnImageSelectedListener {
		public void onImageSelected(String path);
	}

}
